﻿using Marknic.TestRunner.Rigging;
using NetduinoHappyHour.Hardware;
using System;

namespace NetduinoHappyHour.Util
{
    /// <summary>
    ///This is a test class for Testing the SystemConfigUpdate
    ///</summary>
    public class UpdateObjectTest
    {
        /// <summary>
        ///A test for updating boolean value by UpdateObject
        ///</summary>
        public void TestPropertyUpdateBool()
        {
            // TODO check
            TestObject test = new TestObject();
            test.IsBool = true;
            new UpdateObject(test, "IsBool", "false", "bool").Execute();
            Assert.IsFalse(test.IsBool);
            new UpdateObject(test, "IsBool", "true", "bool").Execute();
            Assert.IsTrue(test.IsBool);
            new UpdateObject(test, "IsBool", "False", "bool").Execute();
            Assert.IsFalse(test.IsBool);
            new UpdateObject(test, "IsBool", "True", "bool").Execute();
            Assert.IsTrue(test.IsBool);
        }

        /// <summary>
        ///A test for updating int value by UpdateObject
        ///</summary>
        public void TestPropertyUpdateInt()
        {
            TestObject test = new TestObject();
            test.IsInt = 0;
            new UpdateObject(test, "IsInt", "0815", "int").Execute();
            Assert.Equals(815, test.IsInt);
        }

        /// <summary>
        ///A test for updating string value by UpdateObject
        ///</summary>
        public void TestPropertyUpdateString()
        {
            TestObject test = new TestObject();
            new UpdateObject(test, "IsString", "Salvador", "string").Execute();
            Assert.Equals("Salvador", test.IsString);
        }

        /// <summary>
        ///A test for updating string value by UpdateObject using default type
        ///</summary>
        public void TestPropertyUpdateDefault()
        {
            TestObject test = new TestObject();
            new UpdateObject(test, "IsString", "Salvador", "NonExistentType").Execute();
            Assert.Equals("Salvador", test.IsString);
            new UpdateObject(test, "IsString", "Richter", null).Execute();
            Assert.Equals("Richter", test.IsString);
            new UpdateObject(test, "IsString", null, null).Execute();
            Assert.Equals("leer", test.IsString);
        }

        /// <summary>
        ///A test for updating string value by UpdateObject using default type
        ///</summary>
        public void TestPropertyUpdateMany()
        {
            TestObject test = new TestObject();
            test.IsBool = true;
            new UpdateObject(test, "IsString;Salvador;String").Execute();
            Assert.Equals("Salvador", test.IsString);
            new UpdateObject(test, "IsString;Richter;String#IsInt;0815;int#IsBool;false;bool").Execute();
            Assert.Equals("Richter", test.IsString);
            Assert.Equals(815, test.IsInt);
            Assert.IsFalse(test.IsBool);
        }
        
        /// <summary>
        ///A test for updating string value by UpdateObject using default type
        ///</summary>
        public void TestExPropertyUpdateMany()
        {
            TestObject test = new TestObject();
            // using old delimiter character, causes update to fail
            new UpdateObject(test, "IsString,Richter,String#IsInt,0815,int#IsBool,false,bool").Execute();
        }
        
        /// <summary>
        ///A test for updating boolean value by UpdateObject fails
        ///</summary>
        public void TestExPropertyUpdateFails()
        {
            TestObject test = new TestObject();
            new UpdateObject(test, "DoesNotExist", "True", "bool").Execute();
        }

        private class TestObject
        {
            public bool IsBool { get; set; }
            public int IsInt { get; set; }
            public string IsString { get; set; }
        }
    }
}
